﻿using VIFON.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using VIFON.DAL;

namespace VIFON
{
    public partial class frmProducts : Telerik.WinControls.UI.RadForm
    {
        public frmProducts()
        {
            InitializeComponent();
        }
        BLProduct blProduct = new BLProduct();
        BLUnitType blUnitType = new BLUnitType();
        public string keyWord = string.Empty;       
        private static VIFON.BLL.Utilities.ControlMode CurrentMode = VIFON.BLL.Utilities.ControlMode.Normal;

        private void frmVIFON_Load(object sender, EventArgs e)
        {
            ShowToolbar();
            ddkginpackage.Items.Add("5");
            ddkginpackage.Items.Add("25");
            ddkginpackage.Items.Add("40");
            LoadUnitTypes();
            LoadList();
        }

        private void LoadList()
        {
            grvProducts.Rows.Clear();
            var lst = blProduct.GetProducts(keyWord);
            if (lst != null)
            {
                foreach (var l in lst)
                {
                    grvProducts.Rows.Add(l.Id, l.ProductName, l.UnitType.UnitName, string.Format("{0:#,###}", l.Prices),l.Package);
                }
            }
            grvProducts.Refresh();
            this.Refresh();
        }

        private void LoadUnitTypes()
        {
            ddlUnitType.DataSource = blUnitType.GetUnitTypes();
            ddlUnitType.DisplayMember = "UnitName";
            ddlUnitType.ValueMember = "Id";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            CurrentMode = VIFON.BLL.Utilities.ControlMode.New;
            ShowToolbar();
        }

        private void ShowToolbar()
        {
            switch (CurrentMode)
            {
                case VIFON.BLL.Utilities.ControlMode.Normal:
                    txtproductId.Enabled = txtPrices.Enabled = txtName.Enabled = txtName.Enabled = ddlUnitType.Enabled = ddkginpackage.Enabled = false;
                    btnCancel.Visible = false;
                    btnDelete.Visible = true;
                    btnAdd.Visible = true;
                    btnSave.Visible = false;
                    btnEdit.Visible = true;
                    
                    break;
                case VIFON.BLL.Utilities.ControlMode.New:
                    txtproductId.Enabled = txtPrices.Enabled = txtName.Enabled = txtName.Enabled = ddlUnitType.Enabled = ddkginpackage.Enabled = true;
                    btnCancel.Visible = true;
                    btnDelete.Visible = false;
                    btnEdit.Visible = false;
                    btnAdd.Visible = false;
                    btnSave.Visible = true;
                    txtproductId.Focus();
                    ClearText();
                    break;
                case VIFON.BLL.Utilities.ControlMode.Edit:
                    txtPrices.Enabled = txtName.Enabled = txtName.Enabled = ddlUnitType.Enabled = ddkginpackage.Enabled = true;
                    btnCancel.Visible = true;
                    btnDelete.Visible = false;
                    btnEdit.Visible = false;
                    btnAdd.Visible = false;
                    btnSave.Visible = true;
                    txtproductId.Enabled = false;
                    txtName.Focus();
                    break;
            }
        }

        private void ClearText()
        {
            txtproductId.Text = string.Empty;
            txtPrices.Text = txtName.Text = txtName.Text = string.Empty;            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string productName = txtName.Text;
            int unitType = (int)ddlUnitType.SelectedValue;
            string prices = txtPrices.Text;
            //int productPrices = int.Parse(txtPrices.Text);
            string package = ddkginpackage.Text;
            string productId = txtproductId.Text;

            if (string.IsNullOrEmpty(productName) ||
                string.IsNullOrEmpty(prices) || string.IsNullOrEmpty(productId))
            {
                MessageBox.Show(Common.IsNullOrEmpty, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }           
            Product boProduct = new Product
            {
                Id = productId,
                Prices = int.Parse(txtPrices.Text),
                ProductName = productName,
                Unit = unitType,
                Package=package
            };

            //Chế độ chỉnh sửa
            if (CurrentMode == VIFON.BLL.Utilities.ControlMode.New)
            {
                if (blProduct.Insert(boProduct))
                    MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (blProduct.Update(boProduct))
                    MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                else

                    MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            LoadList();
            CurrentMode = VIFON.BLL.Utilities.ControlMode.Normal;
            ShowToolbar();
            ClearText();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CurrentMode = VIFON.BLL.Utilities.ControlMode.Normal;
            ClearText();
            ShowToolbar();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            CurrentMode = VIFON.BLL.Utilities.ControlMode.Edit;
            ShowToolbar();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int rowindex = grvProducts.CurrentCell.RowIndex;
                string id = grvProducts.Rows[rowindex].Cells[0].Value.ToString();
                if (DialogResult.Yes == MessageBox.Show(Common.ConfirmDelete, Common.MessageTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                {
                    if (blProduct.Delete(id))
                    {
                        MessageBox.Show(Common.ProcessSuccess, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show(Common.ProcessError, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    LoadList();
                }
            }
            catch
            {
                MessageBox.Show(Common.SelectRowData, Common.MessageTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void grvVIFON_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            try
            {
                if (CurrentMode != BLL.Utilities.ControlMode.New)
                {
                    int rowindex = grvProducts.CurrentCell.RowIndex;
                    txtproductId.Text = grvProducts.Rows[rowindex].Cells[0].Value.ToString();
                    txtName.Text = grvProducts.Rows[rowindex].Cells[1].Value.ToString();
                    ddlUnitType.SelectedValue = grvProducts.Rows[rowindex].Cells[2].Value.ToString();
                    string prices = grvProducts.Rows[rowindex].Cells[3].Value.ToString().ToString();
                    prices = prices.Replace(",", string.Empty);
                    txtPrices.Text = prices;
                    if (grvProducts.Rows[rowindex].Cells[4].Value==null)
                    {
                        ddkginpackage.Text = string.Empty;
                    }
                    else { ddkginpackage.Text = grvProducts.Rows[rowindex].Cells[4].Value.ToString(); }
                   
                }
            }
            catch { }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            keyWord = txtKeyWord.Text;
            LoadList();
        }

        private void txtPrices_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true; 
        }
    }
}
